<template>
	<view class="wf-box" :style="{ paddingTop: totalheight }">
		<HeadNav titleName="导入钱包" :topheight="navBarHeight" @rightClick="copy">
			<template v-slot:rightIcon>
				<image src="../../static/img/sign-05.png" class="copy-img"></image>
			</template>
		</HeadNav>

		<view class="main">
			<view class="main-box">
				<view class="word-wrap">
					<view class="word-wbox" v-for="(item,index) in words">
						<view>
							{{ item }}
						</view>
					</view>
					
				</view>

				<view class="footer">
					<view @click="next">下一步</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import { getWordsApi } from '@/api/api.js'
	import HeadNav from "@/components/HeadNav"
	import navBarMixin from '@/mixin/navBarMixin.js'
	export default {
		data() {
			return {
				words: [],
				db: ''
			}
		},
		mixins: [navBarMixin],
		components: {
			HeadNav
		},
		methods: {
			back() {
				uni.navigateBack()
			},

			copy() {
				let str = this.words.toString()
				// console.log('str==',str)
				let wordStr = str.replace(/,/g, ' ')
				console.log('wordStr==', wordStr)

				uni.setClipboardData({
					data: wordStr,
					success: () => {
						uni.showToast({
							title: '复制成功',
							icon: 'none'
						})
					}
				})
				// console.log('wordStr++',this.words)

			},

			async getWords() {
				const res = await getWordsApi({
					langueCode: 3
				})

				const { data } = res
				this.words = data

			},
			next() {
				// let str = this.words.toString()
				// console.log('this.str', str)
				const arr = []
				this.words.forEach((item, index) => {
					arr.push({
						text: item,
						parentIndex: index
					})
				})
				console.log('arr==', arr)

				// brr 是乱序数组
				const brr = this.shuffleArray(JSON.parse(JSON.stringify(arr)))

				brr.forEach((item, index) => {
					item.selfIndex = index
				})

				// console.log('brr==',brr)
				// console.log(JSON.stringify(brr))
				// const cxBrr = JSON.stringify(brr)
				uni.navigateTo({
					url: '/pages/login/validate?data=' + JSON.stringify(brr) + '&db=' + this.db
				})
			},

			// 打乱数组方法
			shuffleArray(array) {
				for (let i = array.length - 1; i > 0; i--) {
					const j = Math.floor(Math.random() * (i + 1));
					[array[i], array[j]] = [array[j], array[i]]; // 数组解构赋值，实现交换
				}
				return array;
			}

		},
		onLoad(option) {

			console.log('oooo===', option.data)
			console.log('xxxx====', JSON.parse(option.data))

			this.db = option.data
			this.getWords()
		},
	}
</script>

<style lang="scss" scoped>
	.wf-box {
		overflow: hidden;

		.back {
			image {
				width: 20rpx;
				height: 37rpx;
			}
		}

		.copy-img {
			width: 48rpx;
			height: 48rpx;
		}

		.main {
			margin-top: 30rpx;
			padding: 0 30rpx;

			.main-box {
				position: relative;
				min-height: 974rpx;
				padding: 40rpx 30rpx;
				background: #fff;
				border-radius: 10rpx;
				box-sizing: border-box;

				.word-wrap {
					min-height: 288rpx;
					display: flex;
					justify-content: space-between;
					flex-wrap: wrap;
					margin: 0 -7rpx;
					// background: #F8F8F8;
					border-radius: 10rpx;
					
					.word-wbox {
						width: 25%;
						padding: 0 7rpx;
						box-sizing: border-box;
						
						&>view {
							margin: 15rpx 0;
							height: 70rpx;
							line-height: 70rpx;
							background: #eee;
							border-radius: 6rpx;
							color: #333;
							position: relative;
							text-align: center;
							font-size: 28rpx;
						}
					}

				}

				.footer {
					bottom: 70rpx;
					padding: 0 20rpx;
					// width: ;
					margin-top: 480rpx;
					box-sizing: border-box;

					&>view {
						line-height: 80rpx;
						text-align: center;
						border-radius: 40rpx;
						background: $theme-color-a;
						color: #fff;
					}
				}
			}
		}
	}
</style>